package Job;

import java.util.TimerTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * description:AtomXXX可以保证原子性吗
 * author:zcx
 * lenovo
 * 时间：2022.02.08.14.18
 */
public class J8 {
    static AtomicBoolean flag=new AtomicBoolean(true);
    static boolean flag2=true;
    static volatile boolean flag3=true;


    public static void main(String[] args) {
        //线程一
        new Thread(()->{
            String tn=Thread.currentThread().getName();
            System.out.printf("线程启动：%s %n",tn);
            while (flag2){

            }
            System.out.printf("线程结束： %s %n",tn);
        },"T1").start();
//main线程
        try {
            TimeUnit.SECONDS.sleep(3);
            flag2=false;
            System.out.println("Main设置了 flag2=false;");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void ato(String[] args) {
        //线程一
        new Thread(()->{
            String tn=Thread.currentThread().getName();
            System.out.printf("线程启动：%s %n",tn);
            while (flag.get()){

            }
            System.out.printf("线程结束： %s %n",tn);
        },"T1").start();
//main线程
        try {
            TimeUnit.SECONDS.sleep(3);
            flag.set(false);
            System.out.println("Main设置了 flag.set(false);");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
